package com.qxsx.midi.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.me.AppVideoAddActivity;
import com.qxsx.midi.model.MyFile;

import java.util.List;

public class MyAdapter extends BaseAdapter {
	private Context context;
	private List<MyFile> data;

	public MyAdapter(Context context, List<MyFile> data) {
		super();
		this.context = context;
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;

		if (null == convertView) {
			viewHolder = new ViewHolder();

			LayoutInflater mInflater = LayoutInflater.from(context);
			convertView = mInflater.inflate(R.layout.listview, null);

			viewHolder.tv_name = (TextView) convertView
					.findViewById(R.id.tv_fileName);
			viewHolder.iv_image = (ImageView) convertView
					.findViewById(R.id.iv_fileImage);
			viewHolder.tv_size = (TextView) convertView
					.findViewById(R.id.tv_fileSize);
			viewHolder.tv_time = (TextView) convertView
					.findViewById(R.id.tv_fileTime);
			viewHolder.ll_listView = (LinearLayout) convertView
					.findViewById(R.id.ll_listView);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		for (int i = data.get(position).getFileName().length(); --i >= 0;) {

			String b = data.get(position).getFileName().substring(i, i + 1);
			boolean c = java.util.regex.Pattern.matches("[\u4E00-\u9FA5]", b); // 判断字符串中是否有汉字
			if (c) {
				if (data.get(position).getFileName().length() > 16) {
					viewHolder.tv_name.setText(data.get(position).getFileName()
							.substring(0, 15)
							+ "...");
				} else {
					viewHolder.tv_name
							.setText(data.get(position).getFileName());
				}
				break;
			} else {
				if (data.get(position).getFileName().length() > 40) {
					viewHolder.tv_name.setText(data.get(position).getFileName()
							.substring(0, 39)
							+ "...");
				} else {
					viewHolder.tv_name
							.setText(data.get(position).getFileName());
				}
			}
		}

		// Bitmap bitmap = BitmapFactory.decodeByteArray(data.get(position)
		// .getFileImage(), 0, data.get(position).getFileImage().length,
		// null);

		Bitmap bitmap = Bytes2Bimap(data.get(position).getFileImage());
		viewHolder.iv_image.setImageBitmap(bitmap);

		viewHolder.tv_size.setText(data.get(position).getFileSize());

		viewHolder.tv_time.setText(data.get(position).getFileTime()
				.substring(0, 10));

		if (AppVideoAddActivity.pos >= 0) {
			if (position == AppVideoAddActivity.pos) {
				viewHolder.ll_listView
						.setBackgroundResource(R.color.item_selected);
			} else {
				viewHolder.ll_listView
						.setBackgroundResource(R.color.item_normor);
			}
		}
		return convertView;
	}

	private static class ViewHolder {
		TextView tv_name;
		ImageView iv_image;
		TextView tv_size;
		TextView tv_time;
		LinearLayout ll_listView;
	}

	public Bitmap Bytes2Bimap(byte[] b) {
		try {

			if (b.length != 0) {
				return BitmapFactory.decodeByteArray(b, 0, b.length);
			} else {
				Bitmap newb = Bitmap.createBitmap(100, 100, Config.RGB_565);
				return newb;
			}
		} catch (Exception e) {
			Bitmap newb = Bitmap.createBitmap(100, 100, Config.RGB_565);
			return newb;
		}
	}
}
